﻿(function () {
    /* Copyright 2010 Imaginary Development. All rights reserved*/
    function DoOnceJquerified() {
        if (typeof ($jq) == 'undefined') {
            if (typeof ($) != 'undefined') {
                $jq = $;
            } else {
                return alert('$jq->jquerify not defined');
            }
        }

        //Analyzer
        var firstRound = $jq('div.battle_log div.round:first');
        var defenders = new Array();
        var attackers = new Array();
        function BaseShip() {
            this.Attack = -1;
            this.Shield = -1;
            this.Hull = -1;
        }
        function GetShipBases(shipClass) {
            var result = new BaseShip();
            switch (shipClass) {

                case 'Atlas_class':
                    result.Shield = 10;
                    result.Hull = 400;
                    break;
                case 'Athena_class':
                    result.Attack = 1000;
                    result.Shield = 200;
                    result.Hull = 6000;
                    break;
                case 'Hades_class':
                    result.Attack = 700;
                    result.Shield = 400;
                    result.Hull = 7000;
                    break;
                case 'Missile_turret':
                    result.Attack = 80;
                    result.Shield = 20;
                    result.Hull = 200;
                    break;
                case 'Laser_turret':
                    result.Attack = 230;
                    result.Shield = 25;
                    result.Hull = 200;
                    break;
                case 'Pulse_cannon':
                    result.Attack = 250;
                    result.Shield = 100;
                    result.Hull = 800;
                    break;
                case 'Particle_cannon':
                    result.Attack = 150;
                    result.Shield = 500;
                    result.Hull = 800;
                    break;
                case 'Decoy':
                    result.Shield = 2000;
                    result.Hull = 2000;
                    break;
                case 'Gauss_cannon':
                    result.Attack = 1100;
                    result.Shield = 200;
                    result.Hull = 3500;
                    break;
                case 'Apollo_class':
                    result.Attack = 150;
                    result.Shield = 25;
                    result.Hull = 850;
                    break;
                case 'Artemis_class':
                    result.Attack = 50;
                    result.Shield = 10;
                    result.Hull = 400;
                    break;
                case 'Solar_satellite':
                    result.Attack = 1;
                    result.Shield = 1;
                    result.Hull = 200;
                    break;
                case 'Charon_class':
                    result.Attack = 1;
                    result.Shield = 25;
                    result.Hull = 800;
                    break;
                case 'Ares_class':
                    result.Attack = 1000;
                    result.Shield = 500;
                    result.Hull = 7500;
                    break;
                case 'Prometheus_class':
                    result.Attack = 2000;
                    result.Shield = 500;
                    result.Hull = 11000;
                    break;
                case 'Hermes_class':
                    result.Attack = 0;
                    result.Shield = 0;
                    result.Hull = 100;
                    break;
                case 'Poseidon_class':
                    result.Attack = 400;
                    result.Shield = 50;
                    result.Hull = 2700;
                    break;
                default:
                    break;
            } // end switch
            return result;
        } //end function Get Ship Bases

        function Combatant() {
            this.WeaponTech = -1;
            this.ArmorTech = -1;
            this.ShieldTech = -1;
            this.IsAssembled = function () {
                return this.WeaponTech >= 0 && this.ArmorTech >= 0 && this.ShieldTech >= 0;
            }
            this.TechOrQ = function (tech) {
                if (tech >= 0)
                    return Math.ceil( tech);
                return '?';
            }
            this.Output = function () {
                return 'W/A/S - ' + this.TechOrQ(this.WeaponTech) + '/' + this.TechOrQ(this.ArmorTech) +
                    '/' + this.TechOrQ(this.ShieldTech);
            };
            this.Assemble = function (shipDom) {
                var shipBases = null;
                function ShipType() {
                    if (shipBases == null) {
                        shipBases = GetShipBases($jq('div.image img', shipDom).attr('alt'));
                    }
                }

                var number = parseInt($jq('div.number:not(.header)', shipDom).text().substring(1).replace(',', ''), 10);

                if (this.WeaponTech < 0 && (shipBases == null || shipBases.Attack > 0)) {
                    var weapon = parseInt($jq('div.attack_strength', shipDom).text().replace(',', ''), 10) / number;

                    if (weapon >= 50) {
                        ShipType();
                        if (shipBases.Attack > 50) {
                            var wTech = (weapon - shipBases.Attack) * 10 / shipBases.Attack;
                            this.WeaponTech = wTech;
                        }
                    }
                }
                if (this.ArmorTech < 0 && (shipBases == null || shipBases.Hull > 0)) {
                    var hull = parseInt($jq('div.hull', shipDom).text().replace(',', ''), 10) / number;
                    ShipType();
                    var aTech = (hull - shipBases.Hull) * 10 / shipBases.Hull;
                    this.ArmorTech = aTech;
                }
                if (this.ShieldTech < 0 && (shipBases == null || shipBases.Shield > 0)) {
                    var shield = parseInt($jq('div.shield_strength', shipDom).text().replace(',', ''), 10) / number;
                    if (shield >= 10) {
                        ShipType();
                        var tech = (shield - shipBases.Shield) * 10 / shipBases.Shield;
                        this.ShieldTech = tech;
                    }
                }

            }; //end function assemble

        }; //end combatant
        function ProcessCombatant(selector, processDom, destination) {
            $jq('div.ship:not(.header)', processDom).each(function (shipIndex, ship) {
                destination.Assemble(ship);
                if (!!destination.IsAssembled()) {
                    return false; // all found abort each loop
                } //end if
            }); // end ship each loop


        };
        function ParseCombatants(selector, destinationArray, combatantIndex) {
            var allCombatantsInSelector = $jq(selector, firstRound);
            allCombatantsInSelector.each(function (index, combatantDom) {
                destinationArray[index] = new Combatant();
                ProcessCombatant(selector, combatantDom, destinationArray[index]);
                var comName = $jq('div.name:eq(' + (index + combatantIndex) + ')', firstRound);
                if (false == !!comName) {
                    alert(selector + '[' + index + ']' + output);
                    throw new ('Could not locate ' + selector + '[' + index + '] header');
                }
                comName.text(comName.text() + destinationArray[index].Output());

            }); // end combatant each loop
        }; //end function ParseCombatants
        ParseCombatants('div.attacker', attackers, 0);
        ParseCombatants('div.defender', defenders, attackers.length);

        // end *analyzer*

    }; //end doOnceJquerified

    var el = document.createElement('div'), b = document.getElementsByTagName('body')[0]; otherlib = false, msg = ''; el.style.position = 'fixed'; el.style.height = '32px'; el.style.width = '220px'; el.style.marginLeft = '-110px'; el.style.top = '0'; el.style.left = '50%'; el.style.padding = '5px 10px 5px 10px'; el.style.zIndex = 1001; el.style.fontSize = '12px'; el.style.color = '#222'; el.style.backgroundColor = '#f99'; if (typeof jQuery != 'undefined') {
        msg = 'This page already using jQuery v' + jQuery.fn.jquery;
        DoOnceJquerified();
        return showMsg();
    } else if (typeof $ == 'function') { otherlib = true; } function getScript(url, success) { var script = document.createElement('script'); script.src = url; var head = document.getElementsByTagName('head')[0], done = false; script.onload = script.onreadystatechange = function () { if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) { done = true; success(); } }; head.appendChild(script); } getScript('http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js', function () {
        if (typeof jQuery == 'undefined') { msg = 'Sorry, but jQuery wasn\'t able to load'; } else {

            msg = 'This page is now jQuerified with v' + jQuery.fn.jquery; if (otherlib) { msg += ' and noConflict(). Use $jq(), not $().'; }
            /* Begin AjaxBookmarklet.js */
            DoOnceJquerified();
            /* End AjaxBookmarklet.js */
        } return showMsg();
    }); function showMsg() { el.innerHTML = msg; b.appendChild(el); window.setTimeout(function () { if (typeof jQuery == 'undefined') { b.removeChild(el); } else { jQuery(el).fadeOut('slow', function () { jQuery(this).remove(); }); if (otherlib) { $jq = jQuery.noConflict(); } } }, 2500); }
})();
